package com.ws.interceptor;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class LoginInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

	private String userId;
	private String userPass;

	public LoginInterceptor(String userId, String userPass) {
		super(Phase.PREPARE_SEND);
		this.userId = userId;
		this.userPass = userPass;
	}

	@Override
	public void handleMessage(SoapMessage msg) throws Fault {
		List<Header> headers = msg.getHeaders();
		Document document = DOMUtils.createDocument();
		Element firstEle = document.createElement("authHeader");
		Element userIdEle = document.createElement("userId");
		userIdEle.setTextContent(userId);
		Element userPassEle = document.createElement("userPass");
		userPassEle.setTextContent(userPass);
		firstEle.appendChild(userIdEle);
		firstEle.appendChild(userPassEle);
		headers.add(new Header(new QName("authHeader"), firstEle));
	}

}
